import {reqToken,reqInfo} from '@/api/index.js'
import { getToken, setToken, removeToken, setTimeStamp } from '@/utils/token'
import router from '@/router'
let state={
    token:getToken(),
    name:''
}


let mutations={
  async  SetToken(state,token){
      state.token=token
      await  setToken(token)
      setTimeStamp()
    },

    SetInfo(state,name){
        state.name=name
    },

    Clear(state){
        state.token='',
        state.name='',
        removeToken()
    }
}


let actions={
    async login({commit}){
        let result=await reqToken()
       await commit('SetToken',result.data.token)
        
    },

    async getInfo({commit}){
        let result=await reqInfo()
        commit('SetInfo',result.data.name)
    },

    async lgout({commit}){
        commit('Clear')
    }
}

export default{
   
    state,
    mutations,
    actions
}